package com.cloud.drore.eboss.supplier.mapper.provider;

import com.cloud.drore.eboos.common.entity.supplier.TicketStock;
import com.cloud.drore.eboos.common.util.DateToStringUtil;
import org.apache.commons.lang.StringUtils;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/1/8 16:23 <br>
 * @Author: <a href="575842778@qq.com">郑永涛</a>
 */
public class TicketStockProvider {

    public TicketStockProvider(){

    }

    private static String selectTicketStockOneTodaySql ="SELECT " +
            "  product.`id` AS common_product_id,product.product_num," +
            "  stock.*,proAsso.distributor_id " +
            " FROM eboos_product product " +
            "  INNER JOIN `eboos_ticket` ticket " +
            "    ON product.product_id = ticket.id " +
            "  INNER JOIN `eboos_ticket_stock` stock " +
            "    ON ticket.`id` = stock.`ticket_id` " +
            "   INNER JOIN `eboos_distributor_product_association` proAsso " +
            "    ON product.id = proAsso.`main_id`" +
            " WHERE product.`product_type` = 'TICKET_PRODUCT' " +
            " AND product.`stock_method` != 'UNLIMITED_STOCK' " +
            "  AND ticket.`is_deleted` = 0 " +
            "  AND stock.`is_deleted` = 0 " +
            "   AND NOW() > stock.start_time " +
            "  AND NOW() < stock.end_time ";

    public String updateTicketStockByParam(TicketStock ticketStock){
        int count = 0;
        String sql = " update eboos_ticket_stock set ";
        if (ticketStock.getInventory()!=null) {
            sql += " inventory =" + ticketStock.getInventory() + "";
            count++;
        }
        if (ticketStock.getStartTime()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " start_time='" + DateToStringUtil.getStringDateTime(ticketStock.getStartTime())  + "'";
            count++;
        }
        if (ticketStock.getEndTime()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " end_time ='" + DateToStringUtil.getStringDateTime(ticketStock.getEndTime()) + "'";
            count++;
        }
        if (ticketStock.getIsDeleted()!=null) {
            if (count != 0) {
                sql += ",";
            }
            sql += " is_deleted ='" + ticketStock.getIsDeleted() + "'";
            count++;
        }
        sql += " where id='" + ticketStock.getId() + "'";
        return sql;
    }


    public String selectTicketStockOneToday(TicketStock ticketStock){
        String sql = selectTicketStockOneTodaySql;
        return sql;
    }


    private static String selectByParamSql = "select id,inventory,ticket_id,start_time,end_time,inventory_type" +
            " from eboos_ticket_stock where is_deleted = '0' ";

    public String selectByParam(TicketStock ticketStock){
        String sql = selectByParamSql;
        if(ticketStock.getId() != null)
            sql += " and id = " + ticketStock.getId() + "";
        if(ticketStock.getTicketId() != null)
            sql += " and ticket_id = " + ticketStock.getTicketId() + "";
        if(ticketStock.getInventory() != null)
            sql += " and inventory = " + ticketStock.getInventory() + "";
        if(StringUtils.isNotBlank(ticketStock.getInventoryType()))
            sql += " and inventory_type = " + ticketStock.getInventoryType() + "";
        return sql;
    }

}